package com.jason.platform.supervision.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.RecordBean;
import com.jason.platform.supervision.beans.RecordResp;
import com.jason.platform.supervision.task.QueryRecordDetailTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.widght.TitleBar;

/**
 * @ClassName: RecordActivity
 * @Description: 电子档案
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:44:30
 */
public class RosterActivity extends BaseActivity implements TaskListener {

    private static final String TAG = RosterActivity.class.getSimpleName();

    public static final String KEY_ROSTER_INFO = "key_roster_info";

    private TitleBar mTitleBar;
    private MyHandler<RosterActivity> mHandler;

    private TextView mCallTextView,mNameTextView,mPhoneTextView,
            mSexTextView,mAgeTextView,mStartTextView,
            mEndTextView,mOffenseTextView,mTypeTextView;

    private RecordBean mRecordBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_roster);
        super.onCreate(savedInstanceState);
        mHandler = new MyHandler<RosterActivity>(this);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mCallTextView = (TextView) findViewById(R.id.roster_call);
        mNameTextView = (TextView) findViewById(R.id.roster_name);
        mPhoneTextView = (TextView) findViewById(R.id.roster_phone);
        mSexTextView = (TextView) findViewById(R.id.roster_sex);
        mAgeTextView = (TextView) findViewById(R.id.roster_age);
        mStartTextView = (TextView) findViewById(R.id.roster_start_time);
        mEndTextView = (TextView) findViewById(R.id.roster_end_time);
        mOffenseTextView = (TextView) findViewById(R.id.roster_offense);
        mTypeTextView = (TextView) findViewById(R.id.roster_type);
        mCallTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = mRecordBean != null ? mRecordBean.getTelephone() : "";
                if (!TextUtils.isEmpty(phone)){
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                    startActivity(intent);
                }else {
                    Toast.makeText(RosterActivity.this,"没有手机号码无法拨打",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mRecordBean = (RecordBean) getIntent().getSerializableExtra(KEY_ROSTER_INFO);
        updateData();
        String rymcId = mRecordBean != null ? (!TextUtils.isEmpty(mRecordBean.getRymcId()) ? mRecordBean.getRymcId() : mRecordBean.getName()) : "";
        QueryRecordDetailTask task = new QueryRecordDetailTask(this);
        task.type = Constant.Manager.GET_RYMC_DETAIL;
        task.mParams.put("rymcId", rymcId);
        task.execute();
    }

    private void updateData(){
        if (mRecordBean != null){
            mNameTextView.setText(mRecordBean.getName());
            mPhoneTextView.setText(mRecordBean.getTelephone());
            mSexTextView.setText(mRecordBean.getSex());
            mAgeTextView.setText(mRecordBean.getCsrq());
            mStartTextView.setText(mRecordBean.getStartTime());
            mEndTextView.setText(mRecordBean.getEndTime());
            mOffenseTextView.setText(mRecordBean.getCriminal());
            mTypeTextView.setText(mRecordBean.getType());
        }
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.Manager.GET_RYMC_DETAIL){
            if (task.isSuccess) {
                RecordResp recordResp = (RecordResp) task.resData;
                mRecordBean = recordResp.getValues().get(0);
                updateData();
            }
        }
    }
}
